<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 钉钉OA消息 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace ding\msg\chat;

/**
 * oa会话消息
 *
 * @package ding\msg\chat
 */
class OaChatMsg extends ChatMsg {
    /**
     * 客户端点击消息时跳转到的H5地址
     */
    protected $messageUrl;
    /**
     * (可选)PC端点击消息时跳转到的H5地址 消息链接能在PC端打开
     * 如果想控制在PC客户端点击链接时，能在PC客户端打开页面而不是外跳到浏览器，可以使用下面的方式：
     * dingtalk://dingtalkclient/page/link?url=http%3A%2F%2Fwww.dingtalk.com&pc_slide=true
     * 其中，url参数代表要打开的链接，必须要做UrlEncode；
     * pc_slide参数，如果为true代表在PC客户端打开，为false或者不写代表用浏览器打开
     */
    protected $pcMessageUrl;
    /**
     * 消息头部的背景颜色。长度限制为8个英文字符，其中前2为表示透明度，后6位表示颜色值。不要添加0x
     */
    protected $headBgColor;
    /**
     * 消息的头部标题（向普通会话发送时有效，向企业会话发送时会被替换为微应用的名字），长度限制为最多10个字符
     */
    protected $headText;
    /**
     * 消息体的标题
     */
    protected $bodyTitle;
    /**
     * 消息体的表单，最多显示6个，超过会被隐藏
     * array(key1=>value1, key2=>value2)
     * @var
     */
    protected $bodyFormItems;
    /**
     * 单行富文本信息的数目
     * @var
     */
    protected $bodyRichNum;
    /**
     * 单行富文本信息的单位
     * @var
     */
    protected $bodyRichUnit;
    /**
     * 消息体的内容，最多显示3行
     * @var
     */
    protected $bodyContent;
    /**
     * 消息体中的图片media_id
     * @var
     */
    protected $bodyImage;
    /**
     * 自定义的附件数目。
     * @var
     */
    protected $bodyFileCount;
    /**
     * 自定义的作者名字
     * @var
     */
    protected $bodyAuthor;

    /**
     * oa消息构造函数
     */
    public function __construct() {
        parent::__construct("oa");
    }

    /**
     * @param mixed $messageUrl
     * @return OaChatMsg
     */
    public function setMessageUrl($messageUrl) {
        $this->messageUrl = $messageUrl;
        return $this;
    }

    /**
     * @param mixed $pcMessageUrl
     * @return OaChatMsg
     */
    public function setPcMessageUrl($pcMessageUrl) {
        $this->pcMessageUrl = $pcMessageUrl;
        return $this;
    }

    /**
     * @param mixed $headBgColor
     * @return OaChatMsg
     */
    public function setHeadBgColor($headBgColor) {
        $this->headBgColor = $headBgColor;
        return $this;
    }

    /**
     * @param mixed $headText
     * @return OaChatMsg
     */
    public function setHeadText($headText) {
        $this->headText = $headText;
        return $this;
    }

    /**
     * @param mixed $bodyTitle
     * @return OaChatMsg
     */
    public function setBodyTitle($bodyTitle) {
        $this->bodyTitle = $bodyTitle;
        return $this;
    }

    /**
     * @param mixed $bodyFormItems
     * @return OaChatMsg
     */
    public function setBodyFormItems($bodyFormItems) {
        $this->bodyFormItems = $bodyFormItems;
        return $this;
    }

    /**
     * @param mixed $bodyRichNum
     * @return OaChatMsg
     */
    public function setBodyRichNum($bodyRichNum) {
        $this->bodyRichNum = $bodyRichNum;
        return $this;
    }

    /**
     * @param mixed $bodyRichUnit
     * @return OaChatMsg
     */
    public function setBodyRichUnit($bodyRichUnit) {
        $this->bodyRichUnit = $bodyRichUnit;
        return $this;
    }

    /**
     * @param mixed $bodyContent
     * @return OaChatMsg
     */
    public function setBodyContent($bodyContent) {
        $this->bodyContent = $bodyContent;
        return $this;
    }

    /**
     * @param mixed $bodyImage
     * @return OaChatMsg
     */
    public function setBodyImage($bodyImage) {
        $this->bodyImage = $bodyImage;
        return $this;
    }

    /**
     * @param mixed $bodyFileCount
     * @return OaChatMsg
     */
    public function setBodyFileCount($bodyFileCount) {
        $this->bodyFileCount = $bodyFileCount;
        return $this;
    }

    /**
     * @param mixed $bodyAuthor
     * @return OaChatMsg
     */
    public function setBodyAuthor($bodyAuthor) {
        $this->bodyAuthor = $bodyAuthor;
        return $this;
    }

    /**
     * 获取消息体
     * {
     *         "message_url": "http://dingtalk.com",
     *         "head": {
     *             "bgcolor": "FFBBBBBB",
     *             "text": "头部标题"
     *         },
     *         "body": {
     *             "title": "正文标题",
     *             "form": [
     *                 {
     *                     "key": "姓名:",
     *                     "value": "张三"
     *                 },
     *                 {
     *                     "key": "年龄:",
     *                     "value": "20"
     *                 },
     *                 {
     *                     "key": "身高:",
     *                     "value": "1.8米"
     *                 },
     *                 {
     *                     "key": "体重:",
     *                     "value": "130斤"
     *                 },
     *                 {
     *                     "key": "学历:",
     *                     "value": "本科"
     *                 },
     *                 {
     *                     "key": "爱好:",
     *                     "value": "打球、听音乐"
     *                 }
     *             ],
     *             "rich": {
     *                 "num": "15.6",
     *                 "unit": "元"
     *             },
     *             "content": "大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本",
     *             "image": "@lADOADmaWMzazQKA",
     *             "file_count": "3",
     *             "author": "李四 "
     *         }
     * }
     * @return array
     */
    public function getMsgContent() {
        $oa = [];
        $oa['message_url'] = $this->messageUrl;
        if (!empty($this->pcMessageUrl)) {
            $oa['pc_message_url'] = $this->pcMessageUrl;
        }
        $oa['head'] = [
            'bgcolor' => $this->headBgColor,
            'text' => $this->headText
        ];
        $oa['body'] = [];
        if (!empty($this->bodyTitle)) {
            $oa['body']['title'] = $this->bodyTitle;
        }
        if (!empty($this->bodyFormItems)) {
            $oa['body']['form'] = [];
            foreach ($this->bodyFormItems as $key => $value) {
                $oa['body']['form'][] = [
                    'key' => $key,
                    'value' => $value
                ];
            }
        }
        if (!empty($this->bodyRichNum) || !empty($this->bodyRichUnit)) {
            $oa['body']['rich'] = [];
            if (!empty($this->bodyRichNum)) {
                $oa['body']['rich']['num'] = $this->bodyRichNum;
            }
            if (!empty($this->bodyRichUnit)) {
                $oa['body']['rich']['unit'] = $this->bodyRichUnit;
            }
        }
        if (!empty($this->bodyContent)) {
            $oa['body']['content'] = $this->bodyContent;
        }
        if (!empty($this->bodyImage)) {
            $oa['body']['image'] = $this->bodyImage;
        }
        if (!empty($this->bodyFileCount)) {
            $oa['body']['file_count'] = $this->bodyFileCount;
        }
        if (!empty($this->bodyAuthor)) {
            $oa['body']['author'] = $this->bodyAuthor;
        }
        return $oa;
    }

}
